import { ElMessage } from "element-plus";
import { login } from "@/api/api.js";


export default ( { ruleForm, fromRef } ) => {

  const ruleFormRef = ref( fromRef );

  let router = useRouter();
  let store = useStore();

  // 提交按钮
  const submitForm = async ( formEl ) => {
    if ( !formEl ) return;
    await formEl.validate( async ( valid, fields ) => {
      if ( valid ) {
        // console.log( "submit!" );
        let { data, meta } = await login( ruleForm );
        if ( meta.status == 200 ) {
          ElMessage( {
            message: meta.msg,
            type: "success",
          } );
          store.commit( "SAVE_TOKEN", data.token );
          router.push( '/home' );
        } else {
          ElMessage( {
            message: meta.msg,
            type: "error",
          } );
        }
      } else {
        console.log( "error submit!", fields );
      }
    } );
  };
  return {
    submitForm,
    ruleFormRef
  }
}